<?php
/**
 * ShopCartController.class.php
 * 购物车控制器
 * 
 * @author 	王中艺	<wangzy_smile@qq.com>
 * @date 	2017-02-23
 */
namespace Admin\Controller;

class ShopCartController extends BaseController {

	/**
	 * 获取数据列表
	 * @param   $p 页码数
	 */
	function getList(){
		$page 	= $this->iInfo['p'] ? $this->iInfo['p'] : 1;
		$pageHtml	 = '';
		$list 	= D('ShopCart')->getList($page, $pageHtml);
		$this->outOInfo(array('list' => $list, 'page_html' => $pageHtml));
	}

	/**
	 * 获取报表数据
	 * @param   $date_start 起始日期
	 * @param   $date_end 截止日期
	 * @return  info [<description>]
	 */
	function getReport(){
		$timeS 	= $this->iInfo['date_start'] ? strtotime($this->iInfo['date_start']) : strtotime('-15 day');
		$timeE 	= $this->iInfo['date_end'] ? strtotime($this->iInfo['date_end'].' +1 day') : time();

		$Report = new \Admin\Util\Report();
		$data 	= $Report->getShopCart($timeS, $timeE);
		$this->outOInfo($data);
	}

	/**
	 * 交易信息列表
	 * @param   $p 页码数
	 * @param   $keyword 关键词
	 * @param   $date_start 开始时间
	 * @param   $date_end 结束时间
	 * @return  $list json [<description>]
	 */
	function getBargainList(){
		$page 	= $this->iInfo['p'] ? $this->iInfo['p'] : 1;
        $page   = $this->request('p', 1);
        $keyword    = $this->request('keyword', false);
		$timeS 	= $this->iInfo['date_start'] ? 
			strtotime($this->iInfo['date_start']) : false;
		$timeE 	= $this->iInfo['date_end'] ? 
			strtotime($this->iInfo['date_end'].' +1 day') : false;

		$pageHtml 	= true;
		$total 	= true;
		$list 	= D('ShopCart')->getBargainList($keyword, $timeS, $timeE, $page, $pageHtml, $total);
		if($list === false)
			$this->response(D('ShopCart')->errMsg, 0);
		$this->response([
            'list' => $list, 
            'page_html' => $pageHtml, 
            'total' => $total
        ]);
	}

	/**
	 * 导出交易记录
	 * @param   $start 页码数
	 * @param   $length [<description>]
	 * @param   $keyword 关键词
	 * @param   $date_start 开始时间
	 * @param   $date_end 结束时间
	 * @return  $list json [<description>]
	 */
	function outBargainExcel(){
		$start  = isset($this->iInfo['start']) ? $this->iInfo['start'] : false;
        $length     = $this->iInfo['length'] ? $this->iInfo['length'] : 1000;
        $keyword    = $this->iInfo['keyword'] ? $this->iInfo['keyword'] : false;
        $timeS  = $this->iInfo['date_start'] ? 
            strtotime($this->iInfo['date_start']) : false;
        $timeE  = $this->iInfo['date_end'] ? 
            strtotime($this->iInfo['date_end'].' +1 day') : false;

        $data   = D('ShopCart')->outBargainExcel($start, $length, $keyword, $timeS, $timeE);
        if(is_numeric($data))
            $this->outOInfo(array('count' => $data));

        $title  = array(
            '订单ID', '用户昵称', '真实姓名', '用户角色', '物品类型','物品名称', '标准价格', '购买价格', 
            '分成金额', '获得利润', '交易单号', '优惠码', '优惠金额'
        );
        foreach($data as &$item){
            $d['money_id']    = $item['money_id'];
            $d['nickname']  = $item['nickname'];
            $d['truename']  = $item['truename'];
            $d['role_text'] = $item['role_text'];
            $d['goods_type']    = $item['goods_type_text'];
            $d['goods_names']    = $item['goods_names'];
            $d['price']   = $item['price'];
            $d['amount']    = $item['amount'];
            $d['deduct']  = $item['deduct'];
            $d['profit']    = $item['profit'];
            $d['trade_no']    = $item['trade_no'];
            $d['code']   = $item['code'];
            $d['code_amount']   = $item['code_amount'];
            $item   = $d;
        }
        out_excel($title, $data, 'shopcart_list_'.$start);
	}

	/**
	 * 详情交易信息
	 * @param   $shopcart_id 购物车ID
	 * @return  $info json [<description>]
	 */
	function detailBargain(){
		$this->unEmptyParam(array('shopcart_id'));
		$shopcartId 	= $this->iInfo['shopcart_id'];

		$info 	= D('ShopCart')->detailBargainInfo($shopcartId);
		if($info === false)
			$this->outOInfo(D('ShopCart')->errMsg, 0);
		$this->outOInfo(array('money_info' => $info['money_info'], 'deduct_list' => $info['deduct_list']));
	}
}